'use strict';

/**
 * @ngdoc service
 * @name jgappApp.FormSubmit
 * @description
 * # FormSubmit
 * Service in the jgappApp.
 */
angular.module('jgappApp')
    .service('FormSubmit', ['$http', 'openApi', 'Util', function ($http, openApi, Util) {
        var FormSubmit = {};
        FormSubmit.submit = function (options,scope) {
// 后台返回的字段，如果不删除，在提交时候会报错
            delete options.param.fileList
            var url, urlParam, TVtype;
            TVtype = Util.getTVType();
            // 如果是修改页面的话则提交的地址不一样，参数一样
            // Util.DRAFT是由草稿列表进入保存的
            if (TVtype == Util.UPDATE || TVtype == Util.DRAFT) {
                url = openApi.ip + options.urlParam
            } else {
                url = openApi.start
            }
            urlParam = "?operationId=" + options.operationId + "&&processConfigId=" + options.id + "&&url=" + options.urlParam;
            if (!options.operationId) {
                urlParam = options.urlParam
            }
            $http.post(url + urlParam, options.param).then(function (data, status, headers, config) {
                data=data.data;if (data.logout) {
                    Util.logout();
                    return;
                }
                if (data.success) {
                    Util.clearTVType();
                    Util.clearFormData();
                    scope.param={};
                    Util.Toast(data.result, {
                        callback:  function(){
                          Util.goBack();
                          NativeUtil.use("close");
                          scope.$apply()
                        },
                        time: 1000
                    });
                    // 提交成功后刷新列表
                    NativeUtil.use("updateProcess");
                } else {
                  Util.Toast(data.result || "操作失败，请稍后尝试", {
                    time: 5000
                  })
                }
            })
        };
        return FormSubmit;
    }]);
